/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.alliance;

import cz.muni.fi.diplomka.servicelayer.dto.Alliance;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.AlliancesManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.AlliancesManagerImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class JoinPanel extends Panel {

    private AlliancesManager alliancesManager = new AlliancesManagerImpl();
    private List<String> alliances = new ArrayList<>();
    private String selectedAlliance = "";

    public JoinPanel(String id, PageParameters parameters) {
        super(id);

        if (parameters.containsKey("id")) {
            final Long playerId = parameters.getLong("id");

            for (Alliance alliance : alliancesManager.getAllAlliances()) {
                alliances.add(alliance.getName());
            }
            selectedAlliance = alliances.get(0);
            
            final DropDownChoice<String> alliance = new DropDownChoice<>("alliance",
                    new PropertyModel<String>(this, "selectedAlliance"), alliances);

            Form<?> joinAlliance = new Form<Void>("joinAlliance") {
                @Override
                protected void onSubmit() {

                    alliancesManager.joinAlliance(playerId, alliancesManager.getAllianceByName(selectedAlliance).getId());
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", playerId.toString());
                    setResponsePage(AlliancePage.class, pageParameters);
                }
            };

            add(joinAlliance);
            joinAlliance.add(alliance);
        }
    }
}
